package com.eunion.core;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 对图片进行处理
 * 
 * <p>创建日期 2017年2月9日</p>
 *
 */
public class ImageUtils {
	/**
	 * 不改变图片尺寸对图片的大小进行压缩
	 * @param file jpg文件对象
	 * @throws IOException
	 */
	public static void compressJPG(File file) throws IOException{
		BufferedImage bufferedImage= ImageIO.read(file);
		BufferedImage temp=new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
		temp.getGraphics().drawImage(bufferedImage, 0,0, null);
		FileOutputStream out=new FileOutputStream(file);
		ImageIO.write(bufferedImage, "jpg", out);
		out.close();
	}
	
	/**
	 * 不改变图片尺寸对图片的大小进行压缩
	 * @param in 原文件
	 * @param out 生成的新文件
	 * @throws IOException
	 */
	public static void compressJPG(File in,File out) throws IOException{
		BufferedImage bufferedImage= ImageIO.read(in);
		BufferedImage temp=new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
		temp.getGraphics().drawImage(bufferedImage, 0,0, null);
		FileOutputStream outs=new FileOutputStream(out);
		ImageIO.write(bufferedImage, "jpg", outs);
		outs.close();
	}
}
